<template>
  <div class="permission-container">
    <el-button type="primary" size="mini" @click="addPermissionTmp(1,0)">添加权限</el-button>
    <el-table
      :data="tableData"
      :row-class-name="tableRowClassName"
      style="width: 100%"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column
        prop="name"
        label="名称"
        width="180"
      >
      </el-table-column>
      <el-table-column
        prop="code"
        label="标识"
        width="180"
      >
      </el-table-column>
      <el-table-column
        prop="description"
        label="描述"
      >
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template v-slot="{row}">
          <el-button type="text" size="small" @click="addPermissionTmp(2,row.id)">添加</el-button>
          <el-button class="btnEdit" type="text" size="small" @click="updatePermission(row.id)">编辑</el-button>
          <el-popconfirm
            title="这是一段内容确定删除吗？"
            @onConfirm="deletePermission(row.id)"
          >
            <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗 -->
    <el-dialog :title="isEdit?'编辑权限点':'新增权限点'" :visible.sync="isShow" @close="closeDialog">
      <el-form ref="formRef" :model="addForm" :rules="addFromRules">
        <el-form-item label="权限名称" :label-width="'120px'" prop="name">
          <el-input v-model="addForm.name" placeholder="" />
        </el-form-item>
        <el-form-item label="权限标识" :label-width="'120px'" prop="code">
          <el-input v-model="addForm.code" placeholder="" />
        </el-form-item>
        <el-form-item label="权限描述" :label-width="'120px'" prop="description">
          <el-input v-model="addForm.description" type="textarea" placeholder="" />
        </el-form-item>
        <el-form-item label="启用" :label-width="'120px'">
          <el-switch
            v-model="addForm.enVisible"
            :active-value="1"
            :inactive-value="0"
            active-color="#409eff"
          >
          </el-switch>
        </el-form-item>
        <el-form-item :label-width="'120px'">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
          <el-button @click="resetForm">重 置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionListRequest, addPermissionRequest, deletePermissionRequest, updatePermissionRequest, getPermissionDetailRequest } from '@/api/permission'
import { transformListToTree } from '@/utils/index'

export default {

  data() {
    return {
      // 开关弹窗
      isShow: false,
      isEdit: false,
      tableData: [],
      addForm: {
        name: '',
        code: '',
        description: '',
        type: 0,
        pid: 0,
        enVisible: ''
      },
      addFromRules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 初始化数据方法
    async getPermissionList() {
      const res = await getPermissionListRequest()
      this.tableData = transformListToTree(res.data, 0)
    },
    // 判断奇偶行
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2) {
        return 'info-row'
      }
    },
    // 弹窗关闭时
    closeDialog() {
      this.isShow = false
      this.addForm = this.$options.data().addForm
    },
    // 确认
    onSubmit() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            // 修改
            await updatePermissionRequest(this.addForm)
            this.$message.success('修改成功')
          } else {
            // 添加
            await addPermissionRequest(this.addForm)
            this.$message.success('添加成功')
          }
          // 关闭弹窗
          this.closeDialog()
          // 重新渲染
          this.getPermissionList()
        }
      })
    },
    // 删除
    async deletePermission(id) {
      console.log('删')
      await deletePermissionRequest(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    },
    // 编辑
    async updatePermission(id) {
      this.isEdit = true
      this.isShow = true
      // 回显
      const res = await getPermissionDetailRequest(id)
      this.addForm = res.data
    },
    // 重置表单
    resetForm() {
      this.$refs.formRef.resetFields()
    },
    addPermissionTmp(type, pid) {
      this.isShow = true
      this.isEdit = false
      this.addForm.type = type
      this.addForm.pid = pid
    }
  }
}
</script>

<style lang="scss" scoped>
.permission-container{
  padding: 20px;
  .el-table{
    margin-top: 10px;
    ::v-deep .info-row{
      background-color: rgb(255, 255, 244) ;
    }
    .btnEdit{
       margin: 0 10px;
     }
  }
}

</style>
